package com.winhxd.b2c.product.api;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.common.ApiCondition;
import com.winhxd.b2c.common.domain.product.condition.*;
import com.winhxd.b2c.common.domain.product.vo.ProductFilterVO;
import com.winhxd.b2c.common.domain.product.vo.ProductListForAssemblyWebVO;
import com.winhxd.b2c.common.domain.product.vo.ProductQueryWithCategoryVO;
import com.winhxd.b2c.common.domain.product.vo.ProductSpuDetailVO;
import com.winhxd.b2c.common.domain.search.model.ProductDocument;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.product.service.ProductService;
import com.winhxd.b2c.product.service.ProductStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ApiProductController
 * 手机端查询商品信息controller
 * @Author 李中华
 * @Date 2019/1/5 17:24
 * @Description:
 */
@Api(value = "ApiProductController", tags = "Api Product")
@RestController
@RequestMapping(value = "/api-product/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiProductController {
    private static final Logger logger = LoggerFactory.getLogger(ApiProductController.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductStockService productStockService;

    @ApiOperation(value = "C端—查询首页分类")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_800000, message = "参数异常"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")})
    @RequestMapping(value = "category/8001/v1/findProductCategoryList", method = RequestMethod.POST)
    public ResponseResult<List<ProductQueryWithCategoryVO>> findProductCategoryList(@RequestBody ApiCondition condition) {
        logger.info("{} - C端—查询首页分类, 参数：ApiCondition={}", "", condition);
        ResponseResult<List<ProductQueryWithCategoryVO>> result = new ResponseResult<>();
        List<ProductQueryWithCategoryVO> categoryList = productService.findProductQueryWithCategoryList();
        result.setData(categoryList);
        return result;
    }

    @ApiOperation(value = "C端—查询商品列表页面的筛选条件")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")})
    @RequestMapping(value = "category/8002/v1/getProductFilterList", method = RequestMethod.POST)
    public ResponseResult<ProductFilterVO> getProductFilterList(@RequestBody ApiCondition condition) {
        logger.info("{} - C端—查询商品列表页面的筛选条件, 参数：ApiCondition={}", "", condition);
        ResponseResult<ProductFilterVO> result = new ResponseResult<>();
        ProductFilterVO filterList = productService.getProductFilterList();
        result.setData(filterList);
        return result;
    }

    @ApiOperation(value = "C端—根据条件查询商品列表")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @RequestMapping(value = "product/8003/v1/findProductListWithCondition", method = RequestMethod.POST)
    public ResponseResult<List<ProductDocument>> findProductListWithCondition(@RequestBody ProductListQueryCondition condition) {
        logger.info("{} - C端—全文检索查询商品列表页面, 参数：ProductListQueryCondition={}", "", condition);
        ResponseResult<List<ProductDocument>> result = new ResponseResult<>();
        List<ProductDocument> list = productService.findProductListWithCondition(condition);
        result.setData(list);
        return result;
    }


    @ApiOperation(value = "C端—单个商品详情")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @RequestMapping(value = "product/8006/v1/getProductDetail", method = RequestMethod.POST)
    public ResponseResult<ProductSpuDetailVO> getProductDetail(@RequestBody ProductSpuCondition condition) {
        logger.info("{} - C端—单个商品详情, 参数：ProductSpuCondition={}", "", condition);
        ResponseResult<ProductSpuDetailVO> result = new ResponseResult<>();
        ProductSpuDetailVO productSpuDetail = productService.getProductDetail(condition);
        result.setData(productSpuDetail);
        return result;
    }

    @ApiOperation(value = "C端-获取商品列表(带图片)")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @RequestMapping(value = "product/8008/v1/findProductListByPage", method = RequestMethod.POST)
    public ResponseResult<PagedList<ProductListForAssemblyWebVO>> findProductListByPage(@RequestBody ProductCondition condition) {
        ResponseResult<PagedList<ProductListForAssemblyWebVO>> result = new ResponseResult();
        logger.info("C端-获取商品列表(带图片),入参：" + JsonUtil.toJSONString(condition));

        PagedList<ProductListForAssemblyWebVO> list = productService.findProductListForAssemblyByPage(condition);

        logger.info("C端-获取商品列表(带图片),返参：" + JsonUtil.toJSONString(list));
        result.setData(list);
        return result;
    }

}
